package com.ogawa.a7517.fragment;

import android.animation.ValueAnimator;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;

public class BodyTypeFragment extends BaseFragment {
    private ImageView ivPoint1, ivPoint2, ivPoint3, ivPoint4, ivPoint5, ivPoint6;
    private ValueAnimator animator;
    private Dialog errorPosDialog;
    private boolean isShowError;
    private Handler handler = new Handler();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_body_type;
    }

    @Override
    public void initView(View view) {
        ivPoint1 = view.findViewById(R.id.iv_point_1);
        ivPoint2 = view.findViewById(R.id.iv_point_2);
        ivPoint3 = view.findViewById(R.id.iv_point_3);
        ivPoint4 = view.findViewById(R.id.iv_point_4);
        ivPoint5 = view.findViewById(R.id.iv_point_5);
        ivPoint6 = view.findViewById(R.id.iv_point_6);
        animator = ValueAnimator.ofInt(0, 6);
        animator.addUpdateListener(animation -> {
            int animatedValue = (int) animation.getAnimatedValue();
            setPointSelect(animatedValue);
        });
        animator.setDuration(3000);
        animator.setRepeatCount(-1);
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle(getString(R.string.body_type));
        animator.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        animator.pause();
        dismissErrorDialog();
    }

    private void setPointSelect(int pos) {
        ivPoint1.setSelected(pos == 0);
        ivPoint2.setSelected(pos == 1);
        ivPoint3.setSelected(pos == 2);
        ivPoint4.setSelected(pos == 3);
        ivPoint5.setSelected(pos == 4);
        ivPoint6.setSelected(pos == 5);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (animator != null) {
            animator.end();
        }
    }

    private void showErrorDialog() {
        if (errorPosDialog == null) {
            errorPosDialog = new Dialog(activity7517);
            errorPosDialog.getWindow().addFlags(Window.FEATURE_NO_TITLE);
            errorPosDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            errorPosDialog.setContentView(R.layout.dialog_posture_error);
            errorPosDialog.setCancelable(false);
            errorPosDialog.findViewById(R.id.tv_re_detect).setOnClickListener(view -> {
                activity7517.publish(Constant.SHOULDER_DETECT_CHECK, "2");
                handler.postDelayed(() -> isShowError = false, 2000);
                errorPosDialog.dismiss();
            });
        }
        if (!errorPosDialog.isShowing()) {
            if (!isShowError) {
                isShowError = true;
                errorPosDialog.show();
            }
        }
    }

    private void dismissErrorDialog() {
        handler.postDelayed(() -> isShowError = false, 2000);
        if (errorPosDialog != null && errorPosDialog.isShowing()) {
            errorPosDialog.dismiss();
        }
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        if (!isInit) {
            return;
        }
        DeviceStateBean deviceStatusBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (deviceStatusBean != null) {
            DeviceStateBean.FunctionsBean.SeatStatusBean seatStatus = deviceStatusBean.getFunctions().getSeatStatus();
            if (seatStatus != null) {
                int seat = Integer.parseInt(seatStatus.getModelValue());
                if (seat == 0) {
                    showErrorDialog();
                } else {
                    dismissErrorDialog();
                }
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        handler.removeCallbacksAndMessages(null);
    }
}
